{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://mp.weixin.qq.com/s?__biz=MzkxODE3NjExOQ==&mid=2247485333&idx=1&sn=26801986cbf232986c74f6bfa45d4731&chksm=c1b42f8df6c3a69b49553b1216bcd3999e897eec158255ee5d50c20804e2275fdf568c5c1ba8&scene=178&cur_album_id=3354758663365918722#rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入需要使用的库\n",
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 关闭警告信息\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "# 获取沪深300指数10年的收盘价数据\n",
    "start_date = '20140101'  # 开始日期\n",
    "end_date = '20231229'  # 结束日期\n",
    "bars = ak.stock_zh_index_hist_csindex(symbol='000300', start_date=start_date, end_date=end_date)\n",
    "price_df = bars[['日期','收盘']]\n",
    "# 将日期设置为datetime格式\n",
    "price_df['日期'] = pd.to_datetime(price_df['日期'])\n",
    "price_df.set_index('日期', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算日收益率\n",
    "price_df['日收益率'] = price_df['收盘'].pct_change()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算周收益率和月收益率\n",
    "# 计算周收益率：使用 resample 来按周重采样\n",
    "weekly_return = (1 + price_df['日收益率']).resample('W').prod() - 1\n",
    "\n",
    "# 计算月收益率：使用 resample 来按月重采样\n",
    "monthly_return = (1 + price_df['日收益率']).resample('M').prod() - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算累计收益\n",
    "price_df['累计收益率'] = (1 + price_df['日收益率']).cumprod() - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算年化收益率：用自然日天数计算投资年数\n",
    "# 计算投资年数，一年约有365天\n",
    "years = (price_df.index[-1] - price_df.index[0]).days / 365\n",
    "# 计算年化收益率\n",
    "annualized_return = (1 + price_df['累计收益率'][-1])**(1 / years) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算年化收益率，用交易日天数计算投资年数\n",
    "# 计算投资年数，一年约有244个交易日\n",
    "years = len(price_df) / 244\n",
    "# 计算年化收益率\n",
    "annualized_return = (1 + price_df['累计收益率'][-1])**(1 / years) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算对数收益率\n",
    "# 首先，计算所有价格的自然对数\n",
    "log_prices = np.log(price_df['收盘'])\n",
    "# 然后，计算对数价格的差分来获取对数收益率\n",
    "price_df['对数收益率'] = log_prices.diff()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "4broker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
